<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {
    public function login(){
       $this->display();  
    }
    public function dologin(){
       if("" != $_POST['name'] && "" != $_POST['pass']){
            $user = M('user');
            $some = array();
            $some['name'] = trim($_POST['name']);
            $some['pass'] = trim($_POST['pass']);
            $some['role'] = 1;
            $list = $user->where($some)->find();
            if(empty($list)){
                $putout = array();
                $putout['status']=1;
                print json_encode($putout);
                exit;
            }else{
                session("id",$list['id']);
                session("name",$list['name']);
                session("pass",$list['pass']);
                session("islogin",true);
                $putout = array();
                $putout['status']=0;
                print json_encode($putout);
                exit;
            }
        }  else {
            $putout = array();
            $putout['status']=2;
            print json_encode($putout);
            exit;
        }
    }
    public function center(){
        $users = M('user');
        $lists = $users 
                -> where() 
                -> order('id desc')
                -> select();
        $this -> assign('lists',$lists);
        $this -> display();  
    }
    public function userread(){
        $user = M('user');
        $some = array();
        $some['id'] = $_GET['id'];     
        $list = $user
                ->where($some)
                ->find();
        $this -> assign('list',$list);
        $this -> display();  
    }
    public function usersave(){
        if($_POST['pass']==session('pass') && session('pass')!=NULL){
                $user = M('user');
                $data = array();
                $data['role'] = $_POST['role'];
                $some =array();
                $some['id']=$_GET['id'];
                $UD = $user->where($some)->save($data);
                $putout = array();
                $putout['status']=1;
                print json_encode($putout);
                exit;
            }else{
                $putout = array();
                $putout['status']=0;
                print json_encode($putout);
                exit;
            }  
    }
    public function delete(){
        if($_POST['pass']==session('pass') && session('pass')!=NULL){
                $user = M('user');
                $some =array();
                $some['id']=$_GET['id'];
                $user->where($some)->delete();
                $putout = array();
                $putout['status']=1;
                print json_encode($putout);
                exit;
            }else{
                $putout = array();
                $putout['status']=0;
                print json_encode($putout);
                exit;
            }  
    }
    public function module(){
        $module = M('module_lists');
        $lists = $module
                -> table('__MODULE_LISTS__ m')
                -> join('__USER__ AS u ON m.uid=u.id ')
                -> field('m.*,u.name')
                -> select();
        $this -> assign('lists',$lists);
        $this->display();  
    }
    public function moduleread(){
        $module = M('module_lists');
        $some = array();
        $some['id'] = $_GET['id'];
        $list = $module
                -> where($some)-> find();
        $this -> assign('list',$list);
        $this->display();  
    }
    public function modulesave(){
        if($_POST['pass']==session('pass') && session('pass')!=NULL){
                $module = M('module_lists');
                $data = array();
                $data['status'] = $_POST['status'];
                $some =array();
                $some['id']=$_GET['id'];
                $UD = $module->where($some)->save($data);
                $putout = array();
                $putout['status']=1;
                print json_encode($putout);
                exit;
            }else{
                $putout = array();
                $putout['status']=0;
                print json_encode($putout);
                exit;
            }  
    }
    public function moduledelete(){
        if($_POST['pass']==session('pass') && session('pass')!=NULL){
                $module = M('module_lists');
                $some =array();
                $some['id']=$_GET['id'];
                $module->where($some)->delete();
                $putout = array();
                $putout['status']=1;
                print json_encode($putout);
                exit;
            }else{
                $putout = array();
                $putout['status']=0;
                print json_encode($putout);
                exit;
            }  
    }
    public function feedback(){
        $feedback = M('feedback');
        $lists = $feedback -> where() -> select();
        $this -> assign('lists',$lists);
        $this -> display();  
    }
    public function feedbackread(){
        $feedback = M('feedback');
        $some = array();
        $some['id'] = $_GET['id'];
        $list = $feedback -> where($some) -> find();
        $this -> assign('list',$list);
        $this -> display();  
    }
    public function reply() {
        if($_POST['pass']==session('pass') && session('pass')!=NULL){
            $feedback = M('feedback');
            $some = array();
            $some['id'] = $_GET['id'];
            $email = $_POST['email'];
            $body = $_POST['reply'];
            require_once "email.class.php";		
            $smtpserver = "smtp.163.com";//SMTP服务器
            $smtpserverport =25;//SMTP服务器端口
            $smtpusermail = "18683799675@163.com";//SMTP服务器的用户邮箱
            $smtpemailto = $email;//发送给谁
            $smtpuser = "18683799675";//SMTP服务器的用户帐号
            $smtppass = "woyun10010";//SMTP服务器的用户密码
            $mailtitle = "谢谢".$email."的反馈";//邮件主题
            $mailcontent = $body;//邮件内容
            $mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
            $smtp = new \smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
            $smtp->debug = false;//是否显示发送的调试信息
            $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
            $data = array();
            $data['status'] =1;
            $feedback -> where($some) -> save($data);
            $putout = array();
            $putout['status']=1;
            print json_encode($putout);
            exit;
        }else{
            $putout = array();
            $putout['status']=0;
            print json_encode($putout);
            exit;
        }
    }
    public function feedbackdelete(){
        if($_POST['pass'] == session('pass') && session('pass')!=NULL){
            $feedback = M('feedback');
            $some = array();
            $some['id'] = $_GET['id'];
            $feedback -> where($some) ->delete();
            $putout = array();
            $putout['status'] = 1;
            print json_encode($putout);
            exit;
        }else{
            $putout = array();
            $putout['status'] = 0;
            print json_encode($putout);
            exit;
        }
    }
    public function back(){
        session(null);
        header("Location: ".U('Home/Home/home'));
    }
}